/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CONTAINERS_STREAM_ALL_CONTAINER_HPP
#define	_CUBEFS_CONTAINERS_STREAM_ALL_CONTAINER_HPP

#include <cubefs/api/api.hpp>
#include <cubefs/core/container/container.hpp>

#include "names.hpp"

#define CUBEFS_SOURCE "cubefs/containers/stream/all/container.hpp:" CUBEFS_AT

namespace cubefs {namespace ctr {

CUBEFS_CONTAINER_PART_BEGIN(cubefs::ctr::stream::ChecksPartName)

    typedef typename Base::PageManagerType                       				PageManagerType;

    typedef typename PageManagerType::Page                                      Page;
    typedef typename PageManagerType::ID                                        ID;

    typedef typename Base::Types::PageDispatcher                                PageDispatcher;

    static const int PAGE_SIZE                                                  = PageManagerType::PAGE_SIZE;

    virtual bool Check()
    {
        TreeMap *roots = me_.GetRootMap();

        bool check = roots->Check(NULL);

        cubefs::api::Iterator* i = roots->CreateIterator();
        Remover<cubefs::api::Iterator> rm_i(i);
        
        TreeMap::Pair* pair = roots->CreatePairHolder();
        Remover<TreeMap::Pair> rm_pair(pair);

        while (!i->IsFlag(cubefs::api::Iterator::ITER_EOF))
        {
            i->GetValue(pair);

            StaticListOfTyped* 	list 	= TypedCast<StaticListOfTyped>(pair->key());
            Number*         	name    = TypedCast<Number>(list->Get(0));
            TypedIDValue*   	id      = TypedCast<TypedIDValue>(pair->value());

            cubefs::api::Model* model 	= me_.GetCollection(id);

            check = model->Check(NULL) || check;

            if (model->ModelName() != name->GetBigIntValue()) {
                check = true;
                CUBEFS_ERROR(me_, "Model names don't match:", model->ModelName(), name->GetBigIntValue());
            }
            
            i->Next();
        }

        return check;
    }

CUBEFS_CONTAINER_PART_END

}}

#undef CUBEFS_SOURCE

#endif
